var express = require('express');
var mysql = require('mysql');

let query = {
    search: 'select * from `seller` where sellerID=?',
    add: 'insert into `seller`(sellerID,name,address,phoneNum,licenseUrl,' +
        'location,businessHour,`range`,class) values (?,?,?,?,?,?,?,?,?)',
    del: 'delete from `seller` where sellerID=?',
    update: 'update `seller` set name=?,address=?,phoneNum=?,' +
        'licenseUrl=?,location=?,businessHour=?,range=? where sellerID=?',
    all: 'select sellerID from `seller` where class=?',
    All: 'select sellerID from `seller`'
}
var search = function (sellerID,callback){
    //建立连接
    var pool = mysql.createPool({
        host     : '148.70.88.72',
        user     : 'test',
        password : '5280d5e6515378d4',
        database : 'test',
        connectionLimit:100
    });

    pool.query(query.search,[sellerID], function (err, result) {
        if (err) throw err;
        //打印查询结果
        //console.log(result);
        callback(result);
        pool.end();
    })
}
var add = function (seller){
//建立连接
    var connection = mysql.createConnection({
        host     : '148.70.88.72',
        user     : 'test',
        password : '5280d5e6515378d4',
        database : 'test',
        connectionLimit:100
    })
    //启动连接
    connection.connect(function (err){
        if(err)
            console.log(err);
    });
    var arr = [];
    for(let i in seller){
        arr.push(seller[i]);
    }
    console.log(arr);
    connection.query(query.add,arr, function (err, result) {
        if (err) throw err;
        connection.end();
    })
}
var del = function (sellerID){
//建立连接
    var connection = mysql.createConnection({
        host     : '148.70.88.72',
        user     : 'test',
        password : '5280d5e6515378d4',
        database : 'test',
        connectionLimit:100
    })
    //启动连接
    connection.connect(function (err){
        if(err)
            console.log(err);
    });
    connection.query(query.del,[sellerID], function (err, result) {
        if (err) throw err;
        connection.end();
    })
}
var update = function (seller){
//建立连接
    var connection = mysql.createConnection({
        host     : '148.70.88.72',
        user     : 'test',
        password : '5280d5e6515378d4',
        database : 'test'
    })
    //启动连接
    connection.connect(function (err){
        if(err)
            console.log(err);
    });
    connection.query(query.update,[], function (err, result) {
        if (err) throw err;
        connection.end();
    })
}
var all = function (cla,callback){
//建立连接
    var pool = mysql.createPool({
        host     : '148.70.88.72',
        user     : 'test',
        password : '5280d5e6515378d4',
        database : 'test',
        connectionLimit:100
    });

    pool.query(query.all,[cla], function (err, result) {
        if (err) throw err;
        //打印查询结果
        var arr1 = [];
        for(let i in result){
            arr1.push(result[i].sellerID);
        }
        callback(arr1);
        pool.end();
    })
}
var All = function (callback){
//建立连接
    var pool = mysql.createPool({
        host     : '148.70.88.72',
        user     : 'test',
        password : '5280d5e6515378d4',
        database : 'test',
        connectionLimit:100
    });

    pool.query(query.All, function (err, result) {
        if (err) throw err;
        //打印查询结果
        var arr1 = [];
        for(let i in result){
            arr1.push(result[i].sellerID);
        }
        callback(arr1);
        pool.end();
    })
}
exports.search = search;
exports.add = add;
exports.del = del;
exports.update = update;
exports.all = all;
exports.All = All;